package com.lv.LvPercentage.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.StringUtils;
import com.lv.LvPercentage.bean.LvPercentage;
import com.lv.LvPercentage.service.LvPercentageManager;
import com.ztc.util.NumberUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
@Controller
@RequestMapping("lv/percentage")
public class LvPercentageAction {

    @Autowired
    LvPercentageManager lvPercentageManager;

    /**
     * 获取快递业务提成比例表
     * @return
     */
    @RequestMapping("getPercentageInfo")
    @ResponseBody
    public HashMap<String,Object> getPercentageInfo(){
      return JsonWrapper.successWrapper(lvPercentageManager.getLvPercentage());
    }

    /**
     * 保存比例信息
     * @param eid   id
     * @param percentageInfo  比例JSON数据
     * @return
     */
    @RequestMapping("savePercentageInfo")
    @ResponseBody
    public HashMap<String,Object> savePercentageInfo(String eid, String percentageInfo, HttpSession session){
        User user=(User)session.getAttribute("userInfo");
        LvPercentage lvPercentage=lvPercentageManager.get(eid);
        if(lvPercentage==null){
            return JsonWrapper.failureWrapperMsg("操作失败，请稍后重试！");
        }
        if(StringUtils.isBlank(percentageInfo)){
            return JsonWrapper.failureWrapperMsg("比例参数错误！");
        }
        JSONObject jsonObject= JSON.parseObject(percentageInfo);
        if(!jsonObject.containsKey("platformScale")||!jsonObject.containsKey("companyScale")){
            return JsonWrapper.failureWrapperMsg("比例参数不完整！");
        }
        //平台抽成
        BigDecimal platformScale=jsonObject.getBigDecimal("platformScale");
        //快递公司抽成
        BigDecimal companyScale=jsonObject.getBigDecimal("companyScale");
        if (platformScale.compareTo(BigDecimal.ZERO) < 1 || platformScale.compareTo(NumberUitl.ONE_BAI) == 1) {
            return JsonWrapper.failureWrapperMsg("平台抽成比例设置不能小于0或超过100%！");
        }
        if (validateDecimal(platformScale)) {
            return JsonWrapper.failureWrapperMsg("比例设置仅支持两位小数！");
        }
        if (companyScale.compareTo(BigDecimal.ZERO) < 1 || companyScale.compareTo(NumberUitl.ONE_BAI) == 1) {
            return JsonWrapper.failureWrapperMsg("旅游公司抽成比例设置不能小于0或超过100%！");
        }
        if (validateDecimal(companyScale)) {
            return JsonWrapper.failureWrapperMsg("比例设置仅支持两位小数！");
        }
        if ((companyScale.add(platformScale)).compareTo(NumberUitl.ONE_BAI) == 1) {
            return JsonWrapper.failureWrapperMsg("平台抽成和旅游公司抽成比例之和不能超过100%！");
        }
        lvPercentage.setFkModifyUserId(user.getId());
        lvPercentage.setModifyTime(new Date());
        lvPercentage.setPercentageScale(percentageInfo);
        return JsonWrapper.successWrapper("保存成功！");
    }

    //验证两位小数
    private boolean validateDecimal(BigDecimal scale) {
        String[] temp = scale.toPlainString().split("\\.");
        if (temp.length > 1) {
            if (temp[1].toString().length() > 2) {
                return true;
            }
        }
        return false;
    }
}